using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Web.Model;

namespace Web.DataAccess
{
    public class TopNewsQuery
    {
        private User CurrentUser { get; set; }
        private int count;

        public TopNewsQuery(User currentUser, int count)
        {
            CurrentUser = currentUser;
            this.count = count;
        }
        public IList<News> Load(ISession session)
        {
            if (CurrentUser == null)
            {
                return session
                .CreateCriteria<News>().Add(Restrictions.Eq("IsHidden", false))
                .AddOrder(Order.Desc("DateCreated")).SetMaxResults(count)
                .List<News>();
            }
            else if(CurrentUser.IsAdmin)
            {
                return session
                .CreateCriteria<News>()
                .AddOrder(Order.Desc("DateCreated")).SetMaxResults(count)
                .List<News>();
            }
            else
            {
                return session
                .CreateCriteria<News>()
                .Add(Restrictions.Eq("IsHidden", false) || Restrictions.Eq("Author_id", CurrentUser.Id))
                .AddOrder(Order.Desc("DateCreated")).SetMaxResults(count)
                .List<News>();
            }
        }
    }
}